//
//  UIView+TZExtension.m
//  TZOCExtension
//
//  Created by Tim.Z on 2017/1/3.
//  Copyright © 2017年 Tim.Z. All rights reserved.
//

#import "UIView+TZExtension.h"
#import <objc/runtime.h>

static int ktz_VCKey;

@implementation UIView (TZExtension)

- (UIViewController *)tz_VC{
    UIViewController *vc = [self tz_getAssociatedValueForKey:&ktz_VCKey];
    if (vc == nil) {
        for (UIView *view = self; view; view = view.superview) {
            UIResponder *nextResponder = [view nextResponder];
            if ([nextResponder isKindOfClass:[UIViewController class]]) {
                vc = (UIViewController *)nextResponder;
                return (UIViewController *)nextResponder;
            }
        }
    }
    return vc;
}

- (void)setTz_VC:(UIViewController *)tz_VC{
    [self tz_setAssociateWeakValue:tz_VC withKey:&ktz_VCKey];
}

- (void)setTz_x:(CGFloat)tz_x
{
    CGRect frame = self.frame;
    frame.origin.x = tz_x;
    self.frame = frame;
}

- (CGFloat)tz_x
{
    return self.frame.origin.x;
}

- (void)setTz_y:(CGFloat)tz_y
{
    CGRect frame = self.frame;
    frame.origin.y = tz_y;
    self.frame = frame;
}

- (CGFloat)tz_y
{
    return self.frame.origin.y;
}

- (void)setTz_width:(CGFloat)tz_width
{
    CGRect frame = self.frame;
    frame.size.width = tz_width;
    self.frame = frame;
}

- (CGFloat)tz_width
{
    return self.frame.size.width;
}

- (void)setTz_height:(CGFloat)tz_height
{
    CGRect frame = self.frame;
    frame.size.height = tz_height;
    self.frame = frame;
}

- (CGFloat)tz_height
{
    return self.frame.size.height;
}

- (void)setTz_size:(CGSize)tz_size
{
    CGRect frame = self.frame;
    frame.size = tz_size;
    self.frame = frame;
}

- (CGSize)tz_size
{
    return self.frame.size;
}

- (void)setTz_origin:(CGPoint)tz_origin
{
    CGRect frame = self.frame;
    frame.origin = tz_origin;
    self.frame = frame;
}

- (CGPoint)tz_origin
{
    return self.frame.origin;
}

- (CGFloat)tz_left {
    return self.frame.origin.x;
}

- (void)setTz_left:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)tz_top {
    return self.frame.origin.y;
}

- (void)setTz_top:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)tz_right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setTz_right:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}

- (CGFloat)tz_bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setTz_bottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)tz_centerX {
    return self.center.x;
}

- (void)setTz_centerX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}

- (CGFloat)tz_centerY {
    return self.center.y;
}

- (void)setTz_centerY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}

- (CGFloat)tz_visibleAlpha {
    if ([self isKindOfClass:[UIWindow class]]) {
        if (self.hidden) return 0;
        return self.alpha;
    }
    if (!self.window) return 0;
    CGFloat alpha = 1;
    UIView *v = self;
    while (v) {
        if (v.hidden) {
            alpha = 0;
            break;
        }
        alpha *= v.alpha;
        v = v.superview;
    }
    return alpha;
}

#pragma mark - --

- (void)tz_setupViewWithKTZViewType:(KTZViewType)type{
    NSInteger cornerRadius = 2;
    
    switch (type) {
        case KTZViewType_cornerRadius2:
        {
        cornerRadius = 2;
        }
            break;
        case KTZViewType_cornerRadius4:
        {
        cornerRadius = 4;
        }
            break;
        case KTZViewType_coupon:
        {
        cornerRadius = 4;
        self.layer.borderColor = UIColorFromRGB(0xd2d2d2).CGColor;
        self.layer.borderWidth = 0.5;
        self.layer.cornerRadius = cornerRadius;
        self.layer.masksToBounds = NO;
        self.clipsToBounds = NO;
        return;
        }
            break;
            
        default:
            break;
    }
    
    self.layer.cornerRadius = cornerRadius;
    self.layer.masksToBounds = YES;
    self.clipsToBounds = YES;
}

- (void)tz_removeAllSubviews {
        //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    while (self.subviews.count) {
        [self.subviews.lastObject removeFromSuperview];
    }
}

@end
